package controller.customer;

import hansot.to.AddressTO;
import hansot.to.MemberTO;
import hansot.to.MenuTO;
import hansot.to.ZipcodeTO;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import service.hansot.CustomerService;

@Controller
public class CustomerController {
	private CustomerService service;

	public CustomerController(CustomerService service) {
		this.service = service;
	}

	@RequestMapping("/join.do")
	public ModelAndView joinCustomer(HttpServletRequest request,HttpServletResponse response,HttpSession session,MemberTO mto) {
		ModelAndView mv = new ModelAndView();
		try {
			service.joinMember(mto);
			mv.setViewName("/join_success");
			mv.addObject("mto", mto);
			ZipcodeTO zto = service.getZipcodeBySeq(mto.getSeq());
			if(zto.getBunji() != null){
				request.setAttribute("smAddress", zto.getSido()+" "+zto.getGugun()+" "+zto.getDong()+" "+zto.getBunji());
			}else{
				request.setAttribute("smAddress", zto.getSido()+" "+zto.getGugun()+" "+zto.getDong());
			}
			request.setAttribute("zipcode", zto.getZipcode());
			request.setAttribute("login_info", mto);
		} catch (Exception e) {
			mv.setViewName("/error");
			mv.addObject("error_message", e.getMessage());
			e.printStackTrace();
		}
		return mv;
	}
	
	@RequestMapping("/getAddress.do")
	public ModelAndView joinCustomer(HttpServletRequest request,HttpServletResponse response,String dong) throws SQLException{
		ModelAndView mv = new ModelAndView();
		System.out.println("컨트롤러");
		ArrayList<ZipcodeTO> rlist = service.getZipcodeByDong(dong);
		System.out.println(rlist);
		ArrayList list = new ArrayList();
		
		for (ZipcodeTO o : rlist) {
			if(o.getBunji() != null){
				list.add(new AddressTO(o.getSeq(), o.getSido()+" "+o.getGugun()+" "+o.getDong()+" "+o.getBunji(), o.getZipcode()));
			}else{
				list.add(new AddressTO(o.getSeq(), o.getSido()+" "+o.getGugun()+" "+o.getDong(), o.getZipcode()));
			}
		}
		
		mv.addObject("list",list);
		System.out.println(list);
		mv.setViewName("jsonView");
		return mv;
	}
	@RequestMapping("/idCheck.do")
	public void checkId(HttpServletRequest request,HttpServletResponse response,String id) throws SQLException, IOException{
		System.out.println("컨트롤러");
		int check = service.idCheck(id);
		System.out.println(check);
		PrintWriter out = response.getWriter();
		out.print(check);
	}
	@RequestMapping("/login.do")
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response,@RequestParam String loginId,@RequestParam String loginPassword) throws SQLException, IOException{
		ModelAndView mv = new ModelAndView();
		MemberTO mto = new MemberTO(loginId, loginPassword);
		System.out.println(mto);
		System.out.println("컨트롤러");
		try{
			MemberTO check = service.selectMemberById(mto);
			ArrayList<MenuTO> list = service.gradeSearch();
			HttpSession session = request.getSession();
			if(check!=null){
				//패스 워드 비교
				if(mto.getPassword().equals(check.getPassword())){//ID/Password 모두 맞는 경우
					session.setAttribute("login_info", check);
					ZipcodeTO zto = service.getZipcodeBySeq(check.getSeq());
					if(zto.getBunji() != null){
						session.setAttribute("smAddress", zto.getSido()+" "+zto.getGugun()+" "+zto.getDong()+" "+zto.getBunji());
					}else{
						session.setAttribute("smAddress", zto.getSido()+" "+zto.getGugun()+" "+zto.getDong());
					}
					session.setAttribute("zipcode", zto.getZipcode());
					System.out.println(zto);
					System.out.println(check);
					mv.setViewName("메인/main");
					mv.addObject("list", list);
				}else{//패스워드가 틀린 경우
					mv.addObject("error_message", "패스워드가 틀렸습니다. 확인 후 다시 로그인 하세요.");
					mv.setViewName("/main");
				}
			}else{//id가 없는 경우
				mv.addObject("error_message", mto.getMemberId()+"는 없는 ID입니다. 확인 후 다시 로그인 하세요.");
				mv.setViewName("/main");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			mv.addObject("error_message", "로그인 도중 오류 발생 : "+e.getMessage());
			mv.setViewName("/error");
		}
		return mv;
		
	}
	@RequestMapping("/logout.do")
	public ModelAndView logout(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView mv = new ModelAndView();
		
		MemberTO mto = (MemberTO)session.getAttribute("login_info");
		if(mto==null){
			mv.addObject("error_message", "로그인 후 로그아웃 할 수 있습니다. 로그아웃 먼저 하세요");
		}else{
			session.invalidate();
		}
		mv.setViewName("redirect:/mainPage.do");
		
		return mv;
	}
	@RequestMapping("/updateMember.do")
	public ModelAndView updateMember(HttpServletRequest request,HttpServletResponse response,HttpSession session,MemberTO mto){
		System.out.println("오키");
		ModelAndView mv = new ModelAndView();
		try {
			service.updateMember(mto);
			mv.setViewName("/join_success");
			session.setAttribute("login_info", mto);
			System.out.println(mto);
			mv.addObject("mto", mto);
		} catch (Exception e) {
			mv.setViewName("/error");
			mv.addObject("error_message", e.getMessage());
			e.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/memberDelete.do")
	public ModelAndView deleteMember(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView mv = new ModelAndView();
		HttpSession sion = request.getSession();
		MemberTO mto = (MemberTO) sion.getAttribute("login_info");
		try {
			service.removeMemberById(mto.getMemberId());
			mv.setViewName("/main");
			sion.invalidate();
		} catch (SQLException e) {
			mv.setViewName("/error");
			mv.addObject("error_message", e.getMessage());
			e.printStackTrace();
		}
		return mv;
	}
}
